package com.newlink.building.login.ui

import android.util.Log
import android.view.View
import com.newlink.building.login.R
import com.newlink.building.login.common.Module_Login_Constant
import com.newlink.building.login.databinding.ActivityForgetPasswordBinding
import com.newlink.building.login.event.SetKitPwdEvent
import com.newlink.building.login.ui.fragments.Module_Login_ForgetPwdFragment
import com.newlink.building.login.ui.fragments.Module_Login_KitSetPasswordFragment
import com.newlink.building.login.ui.fragments.Module_Login_SetPasswordFragment
import com.newlink.building.login.ui.fragments.Module_Login_VerifyCodeFragment
import com.newlink.building.common_base.base.NL_BaseActivity
import com.newlink.building.obfuscation.Module_Obfuscation_ObfuscationHelper
import com.newlink.building.obfuscation.Module_Obfuscation_ObfuscationInvoker
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * @Author: Jake
 * @Date: 2022/10/27
 * @Description: 忘记密码界面
 */
class Module_Login_ForgetPwdActivityNL : NL_BaseActivity() {
    private var mType = ""
    var mForgetPwdFragment: Module_Login_ForgetPwdFragment? = null
    var mSetPasswordFragment: Module_Login_SetPasswordFragment? = null
    var mKitSetPasswordFragment: Module_Login_KitSetPasswordFragment? = null
    var mVerifyCodeFragment: Module_Login_VerifyCodeFragment? = null
    private var jumpFromRegister = false

//    override fun attachLayoutRes(): Int = R.layout.activity_forget_password

    private lateinit var mBinding: ActivityForgetPasswordBinding
    override fun attachLayoutRes(): View {
        mBinding = ActivityForgetPasswordBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun initData() {
        // 忘记密码页面初始化时的混淆
        Module_Obfuscation_ObfuscationHelper.onMethodEnter("ForgetPwdActivity", "initData")
        Module_Obfuscation_ObfuscationInvoker.invokeRandom("password_reset")

        jumpFromRegister = intent.getBooleanExtra("jumpFromRegister", false)

    }

    override fun initView() {
        mType = Module_Login_Constant.Type.RESET_CHOOSE
        mForgetPwdFragment = Module_Login_ForgetPwdFragment.getInstance()
        if (jumpFromRegister) {
            mBinding.topBarBack.visibility = View.GONE
        }

        mBinding.topBarBack.setOnClickListener {
            onBackPressed()
        };

        supportFragmentManager.beginTransaction()
            .replace(
                R.id.forget_pwd_content,
                mForgetPwdFragment!!,
                Module_Login_Constant.Type.RESET_CHOOSE
            )
            .commit()

        if (jumpFromRegister) {
            mBinding.topBarTitle.text = getString(R.string.user_first_set_password)
            showFragment(Module_Login_Constant.Type.RESET_KIT_ACCOUNT)
        }
    }

    fun showFragment(mType: String) {
        // Fragment切换时的混淆
        Module_Obfuscation_ObfuscationInvoker.invokeSpecific(18, mType)
        Module_Obfuscation_ObfuscationHelper.beforeDatabaseOperation("showFragment", mType)

        Log.e("love", "[$localClassName] -> showFragment : $mType")
        val transaction = supportFragmentManager.beginTransaction()
        hideFragments(transaction)

        when (mType) {
            Module_Login_Constant.Type.RESET_CHOOSE -> {
                if (mForgetPwdFragment == null) {
                    mForgetPwdFragment = Module_Login_ForgetPwdFragment.getInstance()
                    transaction.add(R.id.forget_pwd_content, mForgetPwdFragment!!)
                } else {
                    transaction.show(mForgetPwdFragment!!)
                }
            }

            Module_Login_Constant.Type.RESET_VERIFY -> {
                if (mVerifyCodeFragment == null) {
                    mVerifyCodeFragment = Module_Login_VerifyCodeFragment.getInstance("")
                    transaction.add(R.id.forget_pwd_content, mVerifyCodeFragment!!)
                } else {
                    transaction.show(mVerifyCodeFragment!!)
                }
            }

            Module_Login_Constant.Type.RESET_ACCOUNT -> {
                if (mSetPasswordFragment == null) {
                    mSetPasswordFragment = Module_Login_SetPasswordFragment.getInstance("")
                    transaction.add(R.id.forget_pwd_content, mSetPasswordFragment!!)
                } else {
                    transaction.show(mSetPasswordFragment!!)
                }
            }

            Module_Login_Constant.Type.RESET_KIT_ACCOUNT -> {
                if (mKitSetPasswordFragment == null) {
                    mKitSetPasswordFragment = Module_Login_KitSetPasswordFragment.getInstance("")
                    transaction.add(R.id.forget_pwd_content, mKitSetPasswordFragment!!)
                } else {
                    transaction.show(mKitSetPasswordFragment!!)
                }
            }

        }

        transaction.commit()
    }

    private fun hideFragments(transaction: androidx.fragment.app.FragmentTransaction) {
        mForgetPwdFragment?.let { transaction.hide(it) }
        mVerifyCodeFragment?.let { transaction.hide(it) }
        mSetPasswordFragment?.let { transaction.hide(it) }
    }

    override fun onBackPressed() {
        super.onBackPressed()

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onResetPwdEvent(event: SetKitPwdEvent) {
        if (event.tag) {
            this.finish()
        }
    }
}